#include "ansii.h"

#define preamble();   Serial.write(ESCAPE);  Serial.write(BRACE);

void home() {
  preamble();
  Serial.write('H');
}


void xy(int x, int y) {
  preamble();
  Serial.print(y,DEC);
  Serial.write(';');
  Serial.print(x, DEC);
  Serial.write('H');
}


void up(int x) {
  preambleAndNumberAndValue(x,'A');
}


void down(int x) {
  preambleAndNumberAndValue(x,'B');
}


void forward(int x) {
  preambleAndNumberAndValue(x,'C');
}


void backward(int x) {
  preambleAndNumberAndValue(x,'D');
}


void eraseLine() {
  preamble();
  Serial.write('2');
  Serial.write('K');
}


void eraseScreen() {
  preamble();
  Serial.write('1');
  Serial.write('J');
}


void setBackgroundColor(int color) {
  setAttribute(color + 40);
}


void setForegroundColor(int color) {
  setAttribute(color + 30);
}


void boldOn() {
  setAttribute(BOLD_ON);
}


void boldOff() {
  setAttribute(BOLD_OFF);
}


void italicsOn() {
  setAttribute(ITALICS_ON);
}


void italicsOff() {
  setAttribute(ITALICS_OFF);
}


void underlineOn() {
  setAttribute(UNDERLINE_ON);
}


void underlineOff() {
  setAttribute(UNDERLINE_OFF);
}


void strikethroughOn() {
  setAttribute(STRIKETHROUGH_ON);
}


void strikethroughOff() {
  setAttribute(STRIKETHROUGH_OFF);
}


void inverseOn() {
  setAttribute(INVERSE_ON);
}


void inverseOff() {
  setAttribute(INVERSE_OFF);
}


void reset() {
  setAttribute(RESET);
}


void defaultBackground() {
  setAttribute(DEFAULT_BACKGROUND);
}


void defaultForeground() {
  setAttribute(DEFAULT_FOREGROUND);
}


void fill(int x1, int y1, int x2, int y2) {
  for (int x = x1; x <= x2; x++)  {
    for (int y = y1; y <= y2; y++) {
      xy(x,y);
      Serial.print(' ');
    }
  }
}

void preambleAndNumberAndValue(int x, char v) {
  preamble();
  Serial.print(x,DEC);
  Serial.write(v);
}


void setAttribute(int a) {
  preambleAndNumberAndValue(a, 'm');
}

